/**
 * This file is part of the CAKIS project.
 * 
 * Copyright (c) 2009-2010 - Florian Arndt, Christian Kerl
 * 
 * @license GNU Lesser General Public License v3 <http://www.opensource.org/licenses/lgpl-3.0.html>
 * @since   04.05.2010
 * @version SVN: $Id: AddNewProductCommand.java 148 2010-05-15 13:29:45Z f.aarndt@web.de $
 */

package org.cakis.client.commands;

import java.rmi.RemoteException;

import javax.microedition.lcdui.Command;

import org.cakis.client.CakisMIDlet;
import org.cakis.client.forms.ProductExistForm;
import org.cakis.client.forms.ProductNotExistForm;
import org.cakis.webservice.Product;
import org.cakis.webservice.StockUnitNotFoundException;

/**
 * AddNewProductCommand
 *
 * @author Florian Arndt
 */
public class AddNewProductCommand extends AbstractCommand {
	private CakisMIDlet midlet;
	private Product newProduct;
	private ProductNotExistForm form;
	private boolean offlineScannedBarcodes;
	
	public AddNewProductCommand(CakisMIDlet midlet,
			ProductNotExistForm form, boolean offlineScannedBarcodes) {
		super("Erstellen", Command.OK, 1);
		this.midlet = midlet;
		this.form = form;
		this.offlineScannedBarcodes = offlineScannedBarcodes;
	}

	public void execute() {
		newProduct = new Product();
		newProduct.setName(form.getProductName());
		newProduct.setStandardQuantity(form.getProductQuantity());
		newProduct.setEanNumber(form.getProductResult());
		
	   	try {
			midlet.getService().addProduct(newProduct);
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (StockUnitNotFoundException e) {
			e.printStackTrace();
		}
		
		if (offlineScannedBarcodes) {
			ProductExistForm productExist = new ProductExistForm(midlet, newProduct, true);
		} else {
			midlet.getProduct();
		}
	}

}
